/**
 * <pre>
 * Title: 		RemotePersistServiceImpl.java
 * Author:		linriqing
 * Create:	 	2012-3-9 下午04:22:25
 * Copyright: 	Copyright (c) 2012
 * Company:		Shenzhen Helper
 * <pre>
 */
package com.hengpeng.itfin.port;

import java.util.Collection;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hengpeng.common.exception.SystemException;
import com.hengpeng.common.persist.QueryParameter;
import com.hengpeng.common.persist.QueryResult;
import com.hengpeng.common.persist.entity.AbstractEntity;
import com.hengpeng.common.persist.query.DynamicQuery;
import com.hengpeng.common.persist.query.SearchResult;
import com.hengpeng.common.persist.query.SqlResult;
import com.hengpeng.itfin.service.ServiceFactory;

/**
 * <pre>
 * 远程持久层接口实现
 * </pre>
 * @author linriqing
 * @version 1.0, 2012-3-9
 */
@Controller("remotePersistServiceFacade")
@RequestMapping(value = "/RemotePersistService")
public class RemotePersistServiceFacade
{
	private final static Log logger = LogFactory.getLog(RemotePersistServiceFacade.class);

	@Autowired
	ServiceFactory servicefactory;

	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@ResponseBody
	public void delete(@RequestBody AbstractEntity entity)
	{
		servicefactory.getPersistService().delete(entity);
	}

	@RequestMapping(value = "/deleteById", method = RequestMethod.POST)
	@ResponseBody
	public <T extends AbstractEntity> void deleteById(@RequestBody QueryParameter<T> queryParameters)
	{
		servicefactory.getPersistService().delete(queryParameters.getType(), queryParameters.getParameters()[0]);
	}

	@RequestMapping(value = "/get", method = RequestMethod.POST)
	@ResponseBody
	public <T extends AbstractEntity> T get(@RequestBody QueryParameter<T> queryParameters)
	{
		return servicefactory.getPersistService().get(queryParameters.getType(), queryParameters.getParameters()[0]);
	}

	@RequestMapping(value = "/find", method = RequestMethod.POST)
	@ResponseBody
	public <T extends AbstractEntity> QueryResult<T> find(@RequestBody QueryParameter<T> queryParameters)
	{
		QueryResult<T> queryResult = new QueryResult<T>();
		DynamicQuery customQuery = queryParameters.getCustomQuery();
		try
		{
			// 限制客户端提交的查询记录页大小,未设pagesize的默认最大为10000
			SqlResult sqlResult = customQuery.getSqlResult();
			Integer pageSize = sqlResult.getPageSize();
			if (pageSize == null || pageSize < 1)
			{
				pageSize = 10000;
				customQuery.setPageSize(pageSize);
			}
			SearchResult<T> result = servicefactory.getPersistService().findByDynamicQueryPage(
					queryParameters.getType(), queryParameters.getCustomQuery());
			queryResult.setResults(result.getResult());
			if (result.getCount() != null)
			{
				queryResult.setTotalCount(result.getCount());
			}

			queryResult.setPageIndex(queryParameters.getCustomQuery().getPageIndex());
			queryResult.setPageSize(queryParameters.getCustomQuery().getPageSize());
			queryResult.setIsSuccess(true);
		}
		catch (Exception e)
		{
			logger.error("执行远程接口查找异常", e);
			queryResult.setIsSuccess(false);
		}
		return queryResult;
	}

	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public <T extends AbstractEntity> List<T> list(@RequestBody Class<T> type)
	{
		return servicefactory.getPersistService().list(type);
	}

	@RequestMapping(value = "/saveOrUpdate", method = RequestMethod.POST)
	@ResponseBody
	public void saveOrUpdate(@RequestBody AbstractEntity entity)
	{
		servicefactory.getPersistService().saveOrUpdate(entity);
	}
	
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	@ResponseBody
	public <T extends AbstractEntity> T save(@RequestBody T entity)
	{
		return servicefactory.getPersistService().save(entity);
	}

	@RequestMapping(value = "/saveAllObject", method = RequestMethod.POST)
	@ResponseBody
	public void saveAllObject(@RequestBody Collection<? extends AbstractEntity> entitys)
	{
		servicefactory.getPersistService().saveAllObject(entitys);
	}

	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public <T extends AbstractEntity> T update(@RequestBody T entity)
	{
		try
		{
			return servicefactory.getPersistService().update(entity);
		}
		catch (Exception e)
		{
			logger.error("执行update错误", e);
			throw new SystemException(e);
		}
	}

	@RequestMapping(value = "/updateAllObject", method = RequestMethod.POST)
	@ResponseBody
	public void updateAllObject(@RequestBody Collection<? extends AbstractEntity> entitys)
	{
		servicefactory.getPersistService().updateAllObject(entitys);
	}
}
